% Feature Selection By Forward Selection
clc;
clearvars;
% AllDataFileName='Data\BcwrdEliminData_ByMAE_All_70NN.mat';
% FitDataFileName='Data\BcwrdEliminData_ByMAE_Fit_70NN.mat';
% AllFeaturesIndex={[2,3,4,5,6,9,11,13,16,17,20,22,23,28,29,30,31,32,33,34,36,37,38,39] % ALL
%                   [2,3,4,5,6,9,11,13,16,17,20,22,23,28,29]};% No Fitting

% AllDataFileName='Data\BcwrdEliminData_ByMAE_UFS_NoB_All_68NN_Run2.mat';
% FitDataFileName='Data\BcwrdEliminData_ByMAE_UFS_NoB_Fit_68NN_Run2.mat';
% AllFeaturesIndex={[2:5,9,11,28:37,39] % ALL
%                   [2:5,9,11,28,29]};% No Fitting

% AllDataFileName='Data\BcwrdEliminData_ByMAE_NoB_All_69NN_Run2.mat';
% FitDataFileName='Data\BcwrdEliminData_ByMAE_NoB_Fit_69NN_Run2.mat';
% AllFeaturesIndex={[1:5,8:11,28:29,30:39] % ALL
%                   [1:5,8:11,28:29]};% No Fitting
AllDataFileName='Data\BcwrdEliminData_ByMAE_NoFS_NoB_All_40NN_Run2.mat';
FitDataFileName='Data\BcwrdEliminData_ByMAE_NoFS_NoB_FitExc_40NN_Run2.mat';
AllFeaturesIndex={[2:5,8:11,28:29,30:39] % ALL
                  [2:5,8:11,28:29]};% No Fitting

All=load(AllDataFileName);
Fit=load(FitDataFileName);


%% Extract Datas
AllRejected=zeros(All.FNum,1);
AllRejected(1)=setdiff(AllFeaturesIndex{1},All.Features{1});
for J=2:All.FNum
    AllRejected(J)=setdiff(All.Features{J-1},All.Features{J});
end
FitRejected=zeros(Fit.FNum,1);
FitRejected(1)=setdiff(AllFeaturesIndex{2},Fit.Features{1});
for J=2:Fit.FNum
    FitRejected(J)=setdiff(Fit.Features{J-1},Fit.Features{J});
end
%% Display Best Features Index
[~,AllBest]=min(All.BestMAE);
[~,FitBest]=min(Fit.BestMAE);
AllSelectNum=16;
SelectedFeaturesAll=All.Features{AllBest};
fprintf('Selected Features for ALL= [%d',SelectedFeaturesAll(1))
fprintf(',%d',SelectedFeaturesAll(2:end))
fprintf(']\n')
SelectedFeaturesFit=Fit.Features{FitBest};
fprintf('Selected Features for Fit= [%d',SelectedFeaturesFit(1))
fprintf(',%d',SelectedFeaturesFit(2:end))
fprintf(']\n')
%%
fprintf('Imporovment for All = %f\n',All.MAEALL-All.BestMAE(AllBest))
fprintf('Imporovment for Fit = %f\n',Fit.MAEALL-Fit.BestMAE(FitBest))
fprintf('Imporovment Percentage for All = %f%%\n',(All.MAEALL-All.BestMAE(AllBest))/All.MAEALL*100)
fprintf('Imporovment Percentage for Fit = %f%%\n',(Fit.MAEALL-Fit.BestMAE(FitBest))/Fit.MAEALL*100)
fprintf('All) Min Imporove = %f ,Last IMprove = %f  ,Next Imporove = %f\n',-max(diff(All.BestMAE(1:AllBest))),All.BestMAE(AllBest-1)-All.BestMAE(AllBest),All.BestMAE(AllBest)-All.BestMAE(AllBest+1))
fprintf('Fit) Min Imporove = %f ,Last IMprove = %f  ,Next Imporove = %f\n',-max(diff(Fit.BestMAE(1:FitBest))),Fit.BestMAE(FitBest-1)-Fit.BestMAE(FitBest),Fit.BestMAE(FitBest)-Fit.BestMAE(FitBest+1))
fprintf('Last Imporovment Percentage for All = %f%% -> %f%%\n',(All.BestMAE(AllBest-1)-All.BestMAE(AllBest))/All.BestMAE(AllBest)*100,(All.BestMAE(AllBest+1)-All.BestMAE(AllBest))/All.BestMAE(AllBest)*100)
fprintf('Last Imporovment Percentage for Fit = %f%% -> %f%%\n',(Fit.BestMAE(FitBest-1)-Fit.BestMAE(FitBest))/Fit.BestMAE(FitBest)*100,(Fit.BestMAE(FitBest+1)-Fit.BestMAE(FitBest))/Fit.BestMAE(FitBest)*100)

display(All.FeaturesName(All.Features{AllBest}))
display(Fit.FeaturesName(Fit.Features{FitBest}))
%% Plot OPtion
BasePlotFileName='Plots\Fig6NewUFS_';
TextSize=12;
InFigsTextSize=10;
SavePlots=1;
%% Plot
figure(1);
subplot(2,1,1);
set(gca,'FontSize',TextSize)
stairs(0:All.FNum+1,[All.MAEALL,All.BestMAE,All.BestMAE(end)],'linewidth',2)
hold on
stairs(AllBest:All.FNum+1,[All.BestMAE(AllBest:All.FNum),All.BestMAE(end)],'r','linewidth',2)
hold off
for i=1:All.FNum
    text(i+0.5,All.BestMAE(i)+0.001-(0.025*(i==All.FNum)),['\it {',All.FeaturesName{AllRejected(i)},'}'],'Rotation',90,'FontSize',InFigsTextSize)
end
text(0.5,All.MAEALL+0.001,'All Features','Rotation',90,'FontSize',InFigsTextSize)

ylim([0.18,0.26])

xlim([-0.5,All.FNum+1.5])
ylabel('MAE');
% xticks([])
text(-3.1,0.26,'A)','FontSize',TextSize,'FontWeight','Bold')
set(gca,'FontSize',TextSize)

subplot(2,1,2)
set(gca,'FontSize',TextSize)
stairs(0:Fit.FNum+1,[Fit.MAEALL,Fit.BestMAE,Fit.BestMAE(end)],'linewidth',2)
hold on
stairs(FitBest:Fit.FNum+1,[Fit.BestMAE(FitBest:Fit.FNum),Fit.BestMAE(end)],'r','linewidth',2)
hold off
for i=1:Fit.FNum
    text(i+0.5,Fit.BestMAE(i)+0.001-(0.037*(i==Fit.FNum))-(0.037*(i==Fit.FNum-1)),['\it {',Fit.FeaturesName{FitRejected(i)},'}'],'Rotation',90,'FontSize',InFigsTextSize)
end
text(0.5,Fit.MAEALL+0.001,'Fitting Excluded','Rotation',90,'FontSize',InFigsTextSize)

ylim([0.2,0.26])

xlim([-0.5,Fit.FNum+1.5])
ylabel('MAE');
% xticks([])
text(-1.8,0.26,'B)','FontSize',TextSize,'FontWeight','Bold')
xlabel('Number of eliminated features');
set(gca,'FontSize',TextSize)
set(gcf,'Position',[50,50,700,450]);
if SavePlots
FileName=[BasePlotFileName,'1_NoB_Run1'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
